package com.cdut.drivingschool;
import java.io.File;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.app.Application;
/**
 * 定义App的全局信息
 * @author ZhaoHB
 *
 */
public class DrivingApplication extends Application {
	public static DisplayImageOptions options = new DisplayImageOptions.Builder()  
    .cacheInMemory(true) // 加载图片时会在内存中加载缓存  
    .cacheOnDisc(true) // 加载图片时会在磁盘中加载缓存  
    .build(); 
	@Override
	public void onCreate() {
		super.onCreate();
		initImageLoader();
	}
	private static DrivingApplication app;
	/**
	 * 单例模式
	 * @return
	 */
	public static DrivingApplication getInstance(){
		if(app == null){
			app = new DrivingApplication();
		}
		return app;
	}
	/**
     * 初始话图片加载模块
     */
    private void initImageLoader() {
        File cacheDir = StorageUtils.getCacheDirectory(this);
        int cacheSize = 5 * 1024 * 1024;
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                this)
                .threadPoolSize(3)
                        // default
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .memoryCache(new LruMemoryCache(cacheSize))
                .discCache(new UnlimitedDiscCache(cacheDir))
                        // default
                .discCacheSize(50 * 1024 * 1024)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs()
                .build();
        ImageLoader.getInstance().init(config);
    }
}
